import 'package:ladder_cash/app/com/plugin/common_plugin.dart';
import 'package:ladder_cash/app/com/view/webview/my_web/my_web_page.dart';
import 'package:ladder_cash/app/comm_key_def.dart';
import 'package:ladder_cash/app/key_mix.dart';
import 'package:ladder_cash/app/page/mine/setting/setting_page.dart';
import 'package:ladder_cash/app/page/product_detail/product_detail_logic.dart';
import 'package:get/get.dart';
import 'package:liuxlcommon/util/re_click.dart';
import 'package:liuxlcommon/util/toast_util.dart';
import 'package:ladder_cash/app/page/home/home_logic.dart';
import 'package:ladder_cash/app/page/product_detail/product_detail_page.dart';

class RouterManage {
  static const SETTING_PAGE = "/settingPage";
  static const PPRODUCT_DETAIL_PAGE = "/productDetail";
  //
  // static const nearbyBanksPage = "/toNearbyBanks";
  // static const loanManagementPage = "/loanManagementPage";
  // static const emiCalculatorPage = "/emiCalculatorPage";
  // static const advEmiCalculatorPage = "/advEmiCalculatorPage";
  // static const exchangeRateCalculatorPage = "/exchangeRateCalculator";
  static RouterManage? _instance;
  static Map localRouterMap = {
    CommKeyDef.settingPage: SETTING_PAGE,
    CommKeyDef.productDetailPage: PPRODUCT_DETAIL_PAGE
  };

  static List<GetPage> pages = [
    GetPage(name: SETTING_PAGE, page: () =>   SettingPage()),
    GetPage(name: PPRODUCT_DETAIL_PAGE, page: () =>   ProductDetailPage()),

  ];

  factory RouterManage.instance() {
    return _instance ?? RouterManage._init();
  }

  RouterManage._init();

  static bool clickable1() {
    if (lastPopTime == null ||
        DateTime.now().difference(lastPopTime!) > const Duration(seconds: 1)) {
      print(lastPopTime);
      lastPopTime = DateTime.now();
      return true;
    } else {
      return false;
    }
  }

  ///跳转页面
  static Future<T?>? to<T>(
    String route, {
    Map<String, dynamic>? arguments,
    int? id,
    bool preventDuplicates = true,
    Map<String, String>? parameters,
  }) async {
    if (!clickable1()) return null;
    if (route.contains("http")) return toWeb(route);

    if (route.contains("?")) {
      Map<String, String>? urlParams = {};

      for (String str in route.split('?')[1].split('&')) {
        List slist = str.split('=');
        String key=slist[0].toString().rhx;
        urlParams[key] = slist[1];
      }
      arguments ??= urlParams;
      route = route.split('?')[0];
    }
    if (localRouterMap[route] != null) route = localRouterMap[route];
    if (route == PPRODUCT_DETAIL_PAGE) {
      await Get.put(ProductDetailLogic()).preInit(arguments: arguments);
    }
    // if (route == nearbyBanksPage) {
    //   CommonPlugin.toNearbyBanks;
    //   return null;
    // }
    return Get.toNamed<T>(route,
        arguments: arguments,
        id: id,
        parameters: parameters,
        preventDuplicates: preventDuplicates);
  }

  static toWeb(String url) {
    if (url.contains(PPRODUCT_DETAIL_PAGE)) {
      Get.until((route) {
        if (route.settings.name == '/') {
          return true;
        }
        return false;
      });
    }
    return Get.to(MyWebPage(url: url));
  }

  static back() {}

  static toTab(index) {
    Get.until((route) {
      if (route.settings.name == '/HomePage') {
        try {
          HomeLogic logic = Get.find<HomeLogic>();
          logic.currentIndex = index;
          logic.pageController.jumpToPage(index);
          logic.update(['bottom']); //pageview 没有做缓存，会自动刷新
        } catch (e) {
          ToastUtils.toast(e);
        }
        return true;
      }
      return false;
    });
  }
}
